package org.jaaksi.test.cardview;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MyActivity extends FragmentActivity
        implements MyAdapter.OnItemClick
{
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		RecyclerView recyclerView = (RecyclerView) findViewById(
		        R.id.recycler_view);
		// 必须设置布局管理器 线性管理器，支持横向、纵向
		// GridLayoutManager 网格布局管理器 StaggeredGridLayoutManager 瀑布就式布局管理器
		recyclerView.setLayoutManager(new LinearLayoutManager(this));
		// 添加add remove item动画
		recyclerView.setItemAnimator(new DefaultItemAnimator());
		// recyclerView不支持 divider属性

		// 添加分割线
//		 DividerItemDecoration decor = new DividerItemDecoration(this,
//		 DividerItemDecoration.VERTICAL_LIST);
		// recyclerView.addItemDecoration(decor);
		recyclerView.addItemDecoration(new SpaceItemDecoration(0));

		List<String> list = new ArrayList<>();
		for (int i = 0; i < 100; i++)
		{
			list.add(String.format("第%03d条数据", i));
		}
		MyAdapter adapter = new MyAdapter(this, list);
		recyclerView.setAdapter(adapter);
		// recyclerView 没有直接的setOnItemClick方法，可以在Adapter中回调出来
		adapter.setOnItemClick(this);
	}

	@Override
	public void onItemClick(View view, int position)
	{
		Toast.makeText(this, "点击了" + position, Toast.LENGTH_SHORT).show();
	}
}
